<template>
  <div>
    <el-form :model="form" label-width="auto" style="max-width: 900px">
      <el-form-item label="用户名">
        <el-input v-model="form.UserName" />
      </el-form-item>
      <el-form-item label="地址">
        <el-cascader size="large" :options="pcaTextArr" v-model="addressArray">
        </el-cascader>
      </el-form-item>
      <el-form-item label="部门">
        <el-select
          v-model="form.DepartmentId"
          placeholder="请选择部门"
          style="width: 150px"
        >
          <el-option label="IT信息部" :value="1" />
          <el-option label="人力资源部" :value="2" />
          <el-option label="财务部" :value="3" />
          <el-option label="销售部" :value="4" />
          <el-option label="行政部" :value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.UserSex">
          <el-radio :value="true">男</el-radio>
          <el-radio :value="false">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="详细地址">
        <el-input v-model="form.UserAddress" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="Updateuser()">修改</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import axios from "axios";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import {
  provinceAndCityData,
  pcTextArr,
  regionData,
  pcaTextArr,
  codeToText,
} from "element-china-area-data";
const router = useRouter();
const route = useRoute();
const form = reactive({
  UserId: route.query.id,
  UserName: "",
  UserPwd: "12345678",
  UserSex: true,
  Province: "",
  City: "",
  County: "",
  DepartmentId: "",
  UserAddress: "",
  Creator: "",
  Updateor: "",
  Updatedate: "",
  CreationDate: "",
  isDeleted: false,
});
const addressArray: any = ref([]);
const GetUsers = () => {
  axios
    .get("https://localhost:44305/api/Backed/GetUserAll", {
      params: {
        UserId: form.UserId,
      },
    })
    .then((res) => {
      form.UserId = res.data.data.userId;
      form.UserName = res.data.data.userName;
      form.UserSex = res.data.data.userSex;
      form.Province = res.data.data.province;
      form.City = res.data.data.city;
      form.County = res.data.data.county;
      form.DepartmentId = res.data.data.departmentId;
      form.UserAddress = res.data.data.userAddress;
      form.Creator = res.data.data.creator;
      form.CreationDate = res.data.data.creationDate;
      form.Updateor = res.data.data.updateor;
      form.Updatedate = res.data.data.updatedate;
      form.isDeleted = res.data.data.isDeleted;
      addressArray.value.push(
        res.data.data.province,
        res.data.data.city,
        res.data.data.county
      );
    });
};
const Updateuser = () => {
  if (form.UserName == "") {
    ElMessage.error("用户名不得为空");
    return;
  }
  form.Province = addressArray.value[0];
  form.City = addressArray.value[1];
  form.County = addressArray.value[2];
  console.log(form);
  axios
    .post("https://localhost:7070/api/Adduser/updateuser", form)
    .then((res) => {
      if (res.data.code == 300) {
        ElMessage.success(res.data.data);
        router.push("/getuser");
      } else {
        ElMessage.error(res.data.message);
      }
    });
};
onMounted(() => {
  GetUsers();
});
</script>

<style scoped>
</style>